using UnityEngine;

public class ThirdPersonCamera : MonoBehaviour
{
    [Header("目标设置")]
    public Transform target; // 要跟随的目标（角色）
    
    [Header("摄像机设置")]
    public float distance = 3f; // 摄像机距离目标的距离
    public float minDistance = 1f; // 最小距离
    public float height = 1.5f; // 摄像机高度偏移
    public float maxHeight = 3f; // 最大高度限制
    public float minHeight = 0.5f; // 最小高度限制
    public float rotationSpeed = 2f; // 鼠标旋转速度
    public float moveSpeed = 5f; // 摄像机移动速度
    
    [Header("视角切换")]
    public bool isFirstPerson = false; // 是否第一人称
    public float firstPersonHeight = 1.2f; // 第一人称摄像机高度
    
    
    private float currentX = 0f;
    private float currentY = 0f;
    private float currentDistance;
    private Vector3 currentVelocity = Vector3.zero;
    private float maxAngle;
    private float minAngle;
    
    void Start()
    {
        // 如果没有设置目标，尝试找到Player
        if (target == null)
        {
            GameObject player = GameObject.FindGameObjectWithTag("Player");
            if (player != null)
                target = player.transform;
        }
        
        // 初始化摄像机角度和距离
        Vector3 angles = transform.eulerAngles;
        currentX = angles.y;
        currentY = angles.x;
        currentDistance = distance;
        
        // 预计算角度限制（只计算一次）
        maxAngle = Mathf.Atan2(maxHeight - height, distance) * Mathf.Rad2Deg;
        minAngle = Mathf.Atan2(minHeight - height, distance) * Mathf.Rad2Deg;
    }
    
    void LateUpdate()
    {
        if (target == null) return;
        
        // 检查V键切换视角
        if (Input.GetKeyDown(KeyCode.V))
        {
            isFirstPerson = !isFirstPerson;
        }
        
        // 获取鼠标输入
        float mouseX = Input.GetAxis("Mouse X") * rotationSpeed;
        float mouseY = Input.GetAxis("Mouse Y") * rotationSpeed;
        
        currentX += mouseX;
        currentY -= mouseY;
        
        if (isFirstPerson)
        {
            // 第一人称模式
            HandleFirstPerson();
        }
        else
        {
            // 第三人称模式
            HandleThirdPerson();
        }
    }
    
    void HandleFirstPerson()
    {
        // 限制Y轴旋转角度（第一人称限制更严格）
        currentY = Mathf.Clamp(currentY, -80f, 80f);
        
        // 第一人称摄像机位置
        Vector3 targetPosition = target.position;
        targetPosition.y += firstPersonHeight;
        
        // 摄像机直接跟随角色位置
        transform.position = targetPosition;
        
        // 摄像机朝向基于鼠标输入
        transform.rotation = Quaternion.Euler(currentY, currentX, 0);
    }
    
    void HandleThirdPerson()
    {
        // 限制Y轴旋转角度（使用预计算的值）
        currentY = Mathf.Clamp(currentY, minAngle, maxAngle);
        
        // 计算目标位置
        Vector3 targetPosition = target.position;
        targetPosition.y += height;
        
        // 计算摄像机位置
        Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
        Vector3 desiredPosition = targetPosition - rotation * Vector3.forward * distance;
        
        // 平滑移动摄像机
        transform.position = Vector3.SmoothDamp(transform.position, desiredPosition, ref currentVelocity, 1f / moveSpeed);
        
        // 让摄像机始终看向目标
        transform.LookAt(targetPosition);
    }
    
}
